class DragItemGenerator extends egret.Sprite {
    // 道具
    private item: egret.Bitmap
    // UI容器
    private containerUI: egret.Sprite = new egret.Sprite()

    public constructor(itemName) {
        super()
        this.settingItem(itemName)
        this.once(egret.Event.ADDED_TO_STAGE, this.init, this)
    }
    private init() {
        const miva = this
        miva.settinSelf()
        miva.settingUI()
    }
    // 道具设置
    private settingItem(itemName) {
        const miva = this
        miva.item = Utils.createBitmapByName(itemName)
        miva.addChild(miva.item)
        // 道具拖动事件处理
        Utils.dragDisplayObject({
            target: miva.item,
            container: miva,
            that: miva,
            isLog: false,
            isDrag: false,
            funObj: {
                moveFun(params) {
                    const {computePoint: {x, y}} = params
                    const maxRight = miva.x + (miva.width * .5) + 30 < miva.stage.stageWidth
                    const maxBottom = miva.y + (miva.width * .5) + 30 < miva.stage.stageHeight
                    if (!maxRight && x > 0) {
                        miva.x += 0
                    } else {
                        miva.x += x
                    }
                    if (!maxBottom && y > 0) {
                        miva.y += 0
                    } else {
                        miva.y += y
                    }
                    miva.zIndex = 999
                    // console.log(miva.x + miva.width, miva.y + miva.height)
                    // console.log(miva.stage.stageWidth, miva.stage.stageHeight)
                    // console.log('宽:%s, 高: %s', miva.width, miva.height)
                    // console.log('x: %s, y: %s', miva.x, miva.y)
                    // console.log(miva.x + (miva.width * .5) < miva.stage.stageWidth)
                },
                endFun(params) {
                    miva.zIndex = 0
                }
            }
        })
    }
    // 道具容器自身设置
    private settinSelf() {
        const miva = this
        miva.anchorOffsetX = miva.width * .5
        miva.anchorOffsetY = miva.height * .5
        miva.x = miva.stage.stageWidth * .5
        miva.y = miva.stage.stageHeight * .5
        Utils.dragDisplayObject({
            target: miva,
            container: miva.parent,
            that: miva.parent,
            isLog: false,
            isDrag: false,
            funObj: {
                startFun(params) {
                    miva.dispatchEvent(new egret.Event('SELF_TOUCH_START'))
                },
                endFun(params) {
                    miva.dispatchEvent(new egret.Event('SELF_TOUCH_END'))
                }
            }
        })
    }
    // UI容器设置
    private settingUI() {
        const miva = this
        const container: egret.Sprite = new egret.Sprite()

        // container.alpha = 0

        miva.addEventListener('SELF_TOUCH_START', () => {
            egret.Tween.removeTweens(container)
            container.alpha = 1
        }, miva)

        miva.addEventListener('SELF_TOUCH_END', () => {
            egret.Tween.get(container).wait(200).to({ alpha: 0 }, 800)
            miva.stage.dispatchEvent(new egret.Event('ITEM_EDIT_FINISH'))
        }, miva)

        miva.addChild(container)
        miva.createBorder(container)
        miva.createResizeBtn(container)
        miva.createCloseBtn(container)
    }
    /** UI-border 边框 */
    private createBorder(container: egret.DisplayObjectContainer) {
        const miva = this
        const {item} = this
        const computeItem = item.width / item.height
        const border: egret.Bitmap = Utils.createBitmapByName('item_border_png')

        Utils.displayObjectSetting({
            target: border,
            width: item.width,
            height: item.height,
        })

        miva.addEventListener('RESIZE', (evt: egret.Event) => {
            const {data} = evt
            // 道具缩放
            border.width = item.width += data
            border.height = item.height = item.width / computeItem

        }, miva)

        // return border
        container.addChild(border)
    }
    /** 缩放按钮 */
    private createResizeBtn(container: egret.DisplayObjectContainer) {
        const miva = this
        const {item} = this
        const btn: egret.Bitmap = Utils.createBitmapByName('item_scale_png')

        Utils.displayObjectSetting({
            target: btn,
            width: 40,
            height: 40,
            x: item.width - 10,
            y: item.height - 10
        })

        // 缩放按钮事件处理器
        Utils.dragDisplayObject({
            target: btn,
            container: container,
            that: miva,
            isLog: false,
            isDrag: false,
            funObj: {
                moveFun(params) {
                    const {computePoint: {x, y}} = params
                    const old = new egret.Point(item.x + item.width, item.y + item.height)
                    const com = new egret.Point(old.x + x, old.y + y)
                    const res = egret.Point.distance(old, com)

                    if (old.x < com.x || old.y < old.y) {
                        miva.dispatchEventWith('RESIZE', false, res, false)
                    } else if (item.width >= 50 || item.height >= 50) {
                        miva.dispatchEventWith('RESIZE', false, -res, false)
                    }

                    egret.Tween.get(miva).to({
                        anchorOffsetX: miva.width * .5,
                        anchorOffsetY: miva.height * .5
                    }, 0)
                }
            }
        })

        miva.addEventListener('RESIZE', (evt: egret.Event) => {
            // 缩放按钮位置调整
            btn.x = item.width - 10
            btn.y = item.height - 10
        }, miva)

        container.addChild(btn)
    }
    /** 关闭按钮设置 */
    private createCloseBtn(container: egret.DisplayObjectContainer) {
        const miva = this
        const {item} = this
        // 关闭按钮
        const btn = Utils.createBitmapByName('item_close_png')

        Utils.displayObjectSetting({
            target: btn,
            width: 40,
            height: 40,
            x: item.width - 10,
            y: -30
        })

        // 开启关闭按钮事件
        btn.touchEnabled = true
        btn.addEventListener(egret.TouchEvent.TOUCH_TAP, closeHandler, miva)
        // 删除按钮的事件处理器
        function closeHandler(evt: egret.TouchEvent) {
            Utils.superStopPropagation(evt)
            miva.parent.removeChild(miva)
        }

        miva.addEventListener('RESIZE', (evt: egret.Event) => {
            // 关闭按钮位置调整
            btn.x = item.width - 10
            btn.y = -30
        }, miva)

        container.addChild(btn)
    }
}